/* *****************************************************************************************************
 * Copyright 2009. Dbwebx Group
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 * *****************************************************************************************************
 */

package org.dbwebx.mojo.flex.generator;

import org.apache.commons.io.FilenameUtils;

import org.granite.generator.Listener;
import org.granite.generator.as3.As3Type;
import org.granite.generator.as3.JavaAs3GroovyConfiguration;
import org.granite.generator.as3.JavaAs3GroovyTransformer;
import org.granite.generator.as3.JavaAs3Input;
import org.granite.generator.as3.JavaAs3Output;
import org.granite.generator.as3.reflect.JavaInterface;
import org.granite.generator.exception.TemplateUriException;
import org.granite.generator.gsp.GroovyTemplate;

import java.io.File;
import java.io.IOException;

import java.util.HashMap;
import java.util.Map;


public class Gas3GroovyTransformer extends JavaAs3GroovyTransformer {
    private String[] outputClasses;
    private String outputPackageName;

    public Gas3GroovyTransformer(
        JavaAs3GroovyConfiguration config, Listener listener, String[] outputClasses, String outPackage) {
        super(config, listener);
        this.outputClasses = outputClasses;
        outputPackageName = outPackage;
    }

    @Override
    protected JavaAs3Output[] getOutputs(JavaAs3Input input)
        throws IOException, TemplateUriException {
        if (matchWildCard(input.getType().getName(), outputClasses)) {
            return super.getOutputs(input);
        }

        return new JavaAs3Output[0];
    }

    @Override
    protected File getOutputFile(JavaAs3Input input, GroovyTemplate template, File outputDir) {
        As3Type as3Type = input.getJavaType().getAs3Type();
        String qualifiedName = as3Type.getQualifiedName();

        if (null != outputPackageName) {
            String resOutPackage = outputPackageName;

            if (template.isBase()) {
                resOutPackage += ".base";
            }

            qualifiedName = qualifiedName.replace(input.getJavaType().getPackageName(), resOutPackage);
        }

        StringBuilder sb =
            (new StringBuilder()).append(outputDir.getAbsolutePath()).append(File.separatorChar)
             .append(qualifiedName.replace('.', File.separatorChar)).append(getOutputFileSuffix(input, template))
             .append(".as");

        return new File(sb.toString());
    }

    private boolean matchWildCard(String className, String[] wildCards) {
        if (wildCards == null) {
            return true;
        }

        for (String wildCard : wildCards) {
            if (FilenameUtils.wildcardMatch(className, wildCard)) {
                return true;
            }
        }

        return false;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected Map getBindings(JavaAs3Input input, JavaAs3Output output) {
        Map bindings = new HashMap();
        bindings.put("gVersion", "1.1.0");

        if (input.getJavaType() instanceof JavaInterface) {
            JavaInterfaceX extendedInterface;
            extendedInterface = new JavaInterfaceX(this, input.getJavaType().getType(), input.getJavaType().getUrl());

            if (null != outputPackageName) {
                extendedInterface = new JavaInterfaceX(
                        this, input.getJavaType().getType(), outputPackageName, input.getJavaType().getUrl());
            }

            if (null != outputPackageName) {
            }

            input.setJavaType(extendedInterface);
        }

        bindings.put("jClass", input.getJavaType());

        return bindings;
    }
}
